﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EntityModule;

namespace Sport.Share
{
    public partial class Site2 : System.Web.UI.MasterPage
    {
        public SportDataContext Service { get { return EntityService.Instance(); } }
        public string StyleTheme
        {
            get
            {
                if (Session[Constant.StyleTheme].IsNotNullOrEmpty())
                    return Session[Constant.StyleTheme].AsString();
                return "background-image: url('Images/Themes/1.png');background-color: rgb(192, 222, 237);background-repeat: no-repeat;background-attachment: fixed";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Guid guid = Session[Constant.UserID].AsGuid();
            Session[Constant.UserPostCount] = Service.News.Count(s => s.UserID == guid);
            Session[Constant.UserThankCount] = Service.UserThanks.Count(s => s.UserID == guid);
            Session[Constant.UserThankTimeCount] = Service.UserThankTimes.Count(s => s.UserThankID == guid);
            form1.Attributes.Add("style", StyleTheme);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("~/ImageUploads");
            string fullPath = string.Empty;
            if (FileUpload1.HasFile)
            {
                fullPath = path + "\\" + FileUpload1.FileName;
                FileUpload1.SaveAs(fullPath);
            }
            string title = txtPost.Text;
            string img = " <p style=\"text-align:center\"> <img style=\"width:200px\" src=\"ImageUploads/" + FileUpload1.FileName + "\"/></p>";
            string imgDetail = " <p style=\"text-align:center\"> <img style=\"width:400px\" src=\"ImageUploads/" + FileUpload1.FileName + "\"/></p>";
            string titlemain = title.Length > 140 ? title.Substring(0, 140) + "..." : title;
            New news = new New
            {
                Tilte = "Tan Xuan",
                Body = titlemain + img,
                User = Service.Users.FirstOrDefault(),
                Link = "http://www.minhquandev.com",
                DateModifier = DateTime.Now
            };
            NewsDetail detail = new NewsDetail
            {
                ID = Guid.NewGuid(),
                New = news,
                Body = title + imgDetail,
                DateModifier = DateTime.Now
            };
            new NewsService().Insert(news);
            Response.Redirect("~/Default.aspx");
        }
    }
}